package br.com.mundodaprata.store.model;

import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@NamedQueries({@NamedQuery(name="clientByName", 
                           query="select c from Client c where c.name like :name"),
               @NamedQuery(name="clientByUser", 
                           query="select c from Client c where c.user.code = :code")                           })
@Entity
@Table
public class Client extends AbstractEntity {
	
	private static final long serialVersionUID = 2L;
	
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	@Column
	private Long code;
	
	@ManyToOne(cascade={CascadeType.ALL})
	@JoinColumn(name="credential")
	private User user;
	
	@Column(nullable=false)
	private String name;
	
	@Column(nullable=false)
	private String document;
	
	@Column(name="document_type", nullable=false)
	private String documentType;
	
	@Embedded
	private Address address;

	@Temporal(TemporalType.DATE)
	private Date birthday;

	@Column
	private String email;

	@Column
	private String phone;
	
	@OneToMany(mappedBy="client")
	private List<Order> orders;
	
	public List<Order> getOrders() {
		return orders;
	}
	
	public void setOrders(List<Order> orders) {
		this.orders = orders;
	}
	
	public Long getCode() {
		return code;
	}
	
	public void setCode(Long code) {
		this.code = code;
	}
	
	public User getUser() {
		if(user == null)
		{
			user = new User();
		}
		return user;
   }
	
	public void setUser(User user) {
      this.user = user;
   }
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDocument() {
		return document;
	}

	public void setDocument(String document) {
		this.document = document;
	}

	public String getDocumentType() {
		return documentType;
	}

	public void setDocumentType(String documentType) {
		this.documentType = documentType;
	}

	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}
	
	public Address getAddress() {
		return address;
	}
	
	public void setAddress(Address address) {
		this.address = address;
	}
	
	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		
		builder.append(getCode())
		       .append(" - ")
		       .append("Nome: ")
		       .append(getName())
		       .append(" Documento: ")
		       .append(getDocument());
		
		return builder.toString();
	}
}